<?php
namespace app\laytemp\table;

use app\laytemp\Layers;

class Tables extends Layers {
    private $tbNameArr = [];
    private $tbName = '';
    private $limits = [10,20,50];
    private $searchKey = '';
    private $editUrl = '';
    public function __construct()
    {
        //注册web组件
        $this->layBody=[
            'table'=>''
        ];
        parent::__construct();
    }

    /**
     * 设置表头
     * @param $field
     * @param $title
     * @param string $width
     * @param false $sort
     * @param string $fixed
     * @param string $templet #imgurl 图片 #edit 编辑
     */
    public function setCol($field,$title,$width='',$sort=false,$fixed='',$templet=''){
        $setTb = [
            'field'=>$field,
            'title'=>$title
        ];
        if($width!=''){
            $setTb['width']=$width;
        }
        if($sort!==false){
            $setTb['sort']=$sort;
        }
        if($fixed!=''){
            $setTb['fixed']=$fixed;
        }
        if($templet!=''){
            $setTb['templet']=$templet;
        }
        array_push($this->tbNameArr,$setTb);
    }
    //设置表名
    public function setTbName($tbName){
        $this->tbName = $tbName;
    }
    //设置分页 10,20,30
    public function setLimits(...$limits){
        $this->limits = json_encode($limits);
    }
    //设置关键词搜索
    public function setSearch($searchKey){
        $this->searchKey = $searchKey;
    }
    //设置编辑url
    public function setEditUrl($url){
        $this->editUrl = $url;
    }

    public function showTable(){
        $assign = [
            'tbNameArr' => json_encode($this->tbNameArr),
            'urlTb' => urlAddons('laytemp@table/table_list',[
                'tbName'=>$this->tbName,
                'searchKey'=>$this->searchKey
            ]),
            'limits' => $this->limits,
            'urlDel' => urlAddons('laytemp@table/del',[
                'tbName'=>$this->tbName
            ]),
            'urlEdit' => $this->editUrl
        ];
        $this->layBody['table']= view('laytemp@table/table',$assign)->getContent();
        return $this;
    }
}
